package com.tutu.service.impl;

import com.tutu.controller.CommonsController;
import com.tutu.core.api.AlarmRuleApiUrls;
import com.tutu.service.AlarmRuleService;
import com.tutu.utils.BaseUtil;
import com.tutu.utils.HttpRequestUtil;
import com.tutu.utils.UnicodeUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * alarmRuleServiceImpl
 * Created by 奕恒 on 2022/12/28 21:50.
 */
@Service
public class AlarmRuleServiceImpl implements AlarmRuleService {

    @Override
    public Map<String, Object> pageByParams(String deviceCode, Integer type, Integer userId, String keyWord, Integer pageNum, Integer pageSize) {
        return HttpRequestUtil.getOnlyData(AlarmRuleApiUrls.base_prefix + AlarmRuleApiUrls.page, "deviceCode=" + deviceCode + "&type=" + type + "&keyWord=" + UnicodeUtil.unicodeEncode(keyWord) + "&pageNum=" + pageNum + "&pageSize=" + pageSize);
    }

    @Override
    public Map<String, Object> getInfoById(Integer id) {
        return HttpRequestUtil.getOnlyData(AlarmRuleApiUrls.base_prefix + AlarmRuleApiUrls.info, "id=" + id);
    }

    @Override
    public Map<String, Object> saveInfo(HttpServletRequest request,
                                        Integer id,
                                        Integer dataType,
                                        String deviceCode,
                                        Integer type,
                                        String codeDictName,
                                        String codeDictUnit,
                                        Double blueNum,
                                        Double yellowNum,
                                        Double redNum,
                                        Integer alarmType,
                                        Integer alarmTime) {
        LogInfoServiceImpl.saveLogInfo(request, "编辑告警规则，ID：" + id + "，数据类型：" + dataType + "，设备编码：" + deviceCode +
                "，类别：" + type + "，参数名称：" + codeDictName + "，参数单位：" + codeDictUnit + "，蓝线值：" + blueNum + "，黄线值：" + yellowNum +
                "，红线值：" + redNum + "，报警类型：" + alarmType + "，报警时间：" + alarmTime);

        String url = AlarmRuleApiUrls.base_prefix + AlarmRuleApiUrls.update;

        if (null == id) url = AlarmRuleApiUrls.base_prefix + AlarmRuleApiUrls.create;

        return HttpRequestUtil.getCompleteData(url, "id=" + id + "&dataType=" + dataType
                + "&deviceCode=" + deviceCode + "&type=" + type + "&codeDictName=" + UnicodeUtil.unicodeEncode(codeDictName)
                + "&codeDictUnit=" + UnicodeUtil.unicodeEncode(codeDictUnit) + "&blueNum=" + blueNum
                + "&yellowNum=" + yellowNum + "&redNum=" + redNum + "&alarmType=" + alarmType + "&alarmTime=" + alarmTime);
    }

    @Override
    public Map<String, Object> deleteById(HttpServletRequest request, Integer id) {
        LogInfoServiceImpl.saveLogInfo(request, "删除告警规则，ID：" + id);

        return HttpRequestUtil.getCompleteData(AlarmRuleApiUrls.base_prefix + AlarmRuleApiUrls.delete, "id=" + id);
    }

    @Override
    public Map<String, Object> batchDel(HttpServletRequest request, String ids) {
        LogInfoServiceImpl.saveLogInfo(request, "批量删除告警规则，IDS：" + ids);

        return HttpRequestUtil.getCompleteData(AlarmRuleApiUrls.base_prefix + AlarmRuleApiUrls.batchDel, "ids=" + ids);
    }

    @Override
    public Map<String, Object> batchAdd(HttpServletRequest request,
                                        String deviceCode,
                                        Integer type,
                                        String codeStr,
                                        String sizeStr,
                                        String blueNumStr,
                                        String yellowNumStr,
                                        String redNumStr,
                                        String typeStr,
                                        String timeStr) {
        LogInfoServiceImpl.saveLogInfo(request, "批量添加告警规则，设备编码：" + deviceCode +
                "，类别：" + type + "，参数名称：" + codeStr + "，参数单位：" + sizeStr + "，蓝线值：" + blueNumStr + "，黄线值：" + yellowNumStr +
                "，红线值：" + redNumStr + "，报警类型：" + typeStr + "，报警时间：" + timeStr);

        return HttpRequestUtil.getCompleteData(AlarmRuleApiUrls.base_prefix + AlarmRuleApiUrls.batchCreate,
                "deviceCode=" + deviceCode + "&type=" + type + "&codeStr=" + UnicodeUtil.unicodeEncode(codeStr) + "&yellowNumStr=" + yellowNumStr
                        + "&sizeStr=" + sizeStr + "&blueNumStr=" + blueNumStr + "&redNumStr=" + redNumStr
                        + "&typeStr=" + typeStr + "&timeStr=" + timeStr);
    }

    @Override
    public Map<String, Object> syncData(HttpServletRequest request) {
        LogInfoServiceImpl.saveLogInfo(request, "同步告警规则");

        if (CommonsController.needSync("alarmRule")) {
            return HttpRequestUtil.getSyncData(9);
        } else {
            return BaseUtil.defaultErrorInfo(null);
        }
    }

    @Override
    public Map<String, Object> cleanRepeatData(HttpServletRequest request) {
        LogInfoServiceImpl.saveLogInfo(request, "清理重复告警规则");

        return HttpRequestUtil.getCompleteData(AlarmRuleApiUrls.base_prefix + AlarmRuleApiUrls.cleanRepeatData, "");
    }
}
